package com.itbaizhan.hbase2hbase;

import org.apache.hadoop.hbase.client.Mutation;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

//<KEYIN, VALUEIN, KEYOUT>
public class Hbase2HbaseReducer extends TableReducer<Text, IntWritable,Text> {
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //当前key对应的单词出现的总次数
        int sum = 0;
        //遍历values，计算当前单词出现的总次数
        for(IntWritable value:values){
            //累加操作
            sum += value.get();
        }
        //创建Put对象
        Put put = new Put(key.toString().getBytes());
        //添加列以及数据
        put.addColumn("cf".getBytes(),"count".getBytes(), Bytes.toBytes(sum));
        //输出
        context.write(key,put);
    }
}
